/**
 *
 * @author javi
 */

package fmap.gui.jTreePersonal;

import fmap.model.MapF;
import fmap.model.Node;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.Vector;


public class TreeModelResultSearch implements TreeModel
{
    private Vector treeModelListeners = new Vector ();
    private MapF root;
    
    public TreeModelResultSearch (MapF _root)
    {
        root = _root;
    }
    
    
    
    protected Vector getChildren (Object node)
    {
        Vector v = new Vector ();
        
        if( node instanceof MapF )
        {
            MapF mapF = (MapF)node;
            
            if( mapF.getVNode ().size () > 0 )
                v = mapF.getVNode ();
            else
            {
                v = new fmap.dao.NodeDao ().selectNodesResultSearch ( mapF.getIdMapF (), -1, mapF.getIdTable () );
                
                mapF.setVNode ( v );
            }
        }
        else if( node instanceof Node )
        {
            Node n = (Node)node;
            
            v = n.getVNode ();
            
            if ( v != null )
            {
                
                if ( v.size () > 0 )
                {
                    v = n.getVNode ();
                }
                else
                {
                    v = new fmap.dao.NodeDao ().selectNodesResultSearch ( n.getIdMapF (), n.getIdNode (), n.getIdTableSub () );
                    n.setNode ( v );
                }
                
            }
            
//            v = d.getDir2 ();
        }
        
        return v;
    }
    
    
//////////////// TreeModel interface implementation ///////////////////////
    
    /**
     * Adds a listener for the TreeModelEvent posted after the tree changes.
     */
    public void addTreeModelListener (TreeModelListener l)
    {
        treeModelListeners.addElement (l);
    }
    
    
    /**
     * Returns the child of parent at index index in the parent's child array.
     */
    public Object getChild (Object parent, int index)
    {
        return getChildren (parent).get (index);
    }
    
    /**
     * Returns the number of children of parent.
     */
    public int getChildCount (Object parent)
    {
        Vector children = getChildren (parent);
        return (children == null) ? 0 : children.size ();
    }
    
    /**
     * Returns the index of child in parent.
     */
    
    public int getIndexOfChild (Object parent, Object child)
    {
        return 1;
    }
    
    /**
     * Returns the root of the tree.
     */
    public Object getRoot ()
    {
        return root;
    }
    
    /**
     * Returns true if node is a leaf.
     */
    public boolean isLeaf (Object node)
    {
        return false;
    }
    
    /**
     * Removes a listener previously added with addTreeModelListener().
     */
    public void removeTreeModelListener (TreeModelListener l)
    {
        treeModelListeners.removeElement (l);
    }
    
    /**
     * Messaged when the user has altered the value for the item
     * identified by path to newValue.  Not used by this model.
     */
    public void valueForPathChanged (TreePath path, Object newValue)
    {
        System.out.println ("*** valueForPathChanged : "
                + path + " --> " + newValue);
    }
}